<template>
  <div>
    <div class="headstyle">
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="grid-content bg-purple">
            <span class="locsty">
              <i class="el-icon-location iconsty"></i>当前位置：综合考试管理
            </span>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="middle">
      <div class="middle">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-button type="primary" icon="el-icon-download">
           <a class="download" href="../../../static/试题导入模板.xlsx" download title="下载">下载试题模板</a>
          </el-button>
        </el-col>
             <el-col :span="8" class="elbuttonedit">
     <el-button type="danger"  icon="el-icon-delete" @click="clearexam">清空试题</el-button>
     </el-col>
        <el-col :span="8" class="editstyle">
          <el-upload
            class="upload-demo"
            ref="upload"
            action="/Interact"
            :file-list="fileList"
            :http-request="uploadSectionFile"
            accept=".xlsx, .xls"
            :auto-upload="false"
          >
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button
              style="margin-left: 10px;"
              size="small"
              type="success"
              @click="submitUpload"
            >上传到服务器</el-button>
            
          </el-upload>
        </el-col>
      </el-row>
          </div>
    </div>

    <div style="margin-top:25px;text-align: center;">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :header-cell-style="{
      'background':'#e6f3f8',
      'font-weight':'800',
      'color':'black',
    }"
      >
        <el-table-column fixed prop="order" label="序号" width="50"></el-table-column>
        <el-table-column prop="type" label="题型" width="80"></el-table-column>
        <el-table-column prop="question" label="题目" width="500"></el-table-column>
        <el-table-column prop="optionA" label="选项A"></el-table-column>
        <el-table-column prop="optionB" label="选项B"></el-table-column>
        <el-table-column prop="optionC" label="选项C"></el-table-column>
        <el-table-column prop="optionD" label="选项D"></el-table-column>
        <el-table-column prop="optionE" label="选项E"></el-table-column>
        <el-table-column prop="optionF" label="选项F"></el-table-column>
        <el-table-column prop="answer" label="答案"></el-table-column>
      </el-table>
      <el-pagination
                    v-if = "paginationShow"
                    @current-change="currentChange"
                    @size-change="sizeChange"
                    :current-page="currentPage"
                    :page-size="pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="total"
            ></el-pagination>
    </div>
    <!-- <div style="background-color:#FFF;align=right">
  
            </div> -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      fileList: [],

      options: [
        {
          time: "5",
          time_val: "5"
        },
        {
          time: "10",
          time_val: "10"
        },
        {
          time: "15",
          time_val: "15"
        },
        {
          time: "20",
          time_val: "20"
        },
        {
          time: "25",
          time_val: "25"
        }
      ],
      tableData: [],
      value: "",
      num: 1,
      pageSize: 10,
      currentPage: 1,
      total: 100,
      paginationShow: true
    };
  },
  mounted: function() {
    this.loadinfo();
  },
  methods: {
    loadinfo() {
      this.$axios
        .get("/Exam/Getpagelist", {
          params: { page: this.currentPage, pageSize: this.pageSize }
        })
        .then(json => {
          this.tableData = JSON.parse(json.data.List);
          this.total = JSON.parse(json.data.totalnum);
          this.value = JSON.parse(json.data.info).examtime;
          this.num = JSON.parse(json.data.info).examnum;
        });
    },

    clearexam() {
      let couid = this.value;
      this.$confirm("确定清空本课程课堂互动试题吗？", "提示").then(() => {
        this.$axios.delete("/Exam/clearexam", { params: {} }).then(json => {
          if (json.data == "OK") {
            this.$message({
              message: "删除试卷成功！",
              type: "success"
            });
            this.loadinfo();
          } else {
            this.$message({
              message: "删除试卷失败！",
              type: "warning"
            });
            this.loadinfo();
          }
        });
      });
    },

    submitUpload() {
      let list = document.getElementsByClassName(
        "el-upload-list__item is-ready"
      );
      if (list.length == 0) {
        this.$message({
          type: "warning",
          message: "请选择需要导入的模板！"
        });
        return;
      }
      this.$refs.upload.submit();
    },
    uploadSectionFile(param) {
      var fileObj = param.file;
      // FormData 对象
      var form = new FormData();
      // 文件对象
      form.append("file", fileObj);
      form.append("couid", this.value);

      this.$axios.post("/Exam/Post", form).then(res => {
        if (res.data == "OK") {
          this.$message({
            type: "success",
            message: "试题导入成功！"
          });
          this.loadinfo();
          this.tableadd(this.value);
        } else {
          this.$message({
            type: "success",
            message: "试题导入失败！"
          });
        }
        this.$refs.upload.clearFiles();
      });
    },

    // 当页容量发生变化时触发
    sizeChange(size) {
      this.pageSize = size;
      // 重新请求数据
      this.loadinfo();
    },
    // 当页数发生变化时触发
    currentChange(num) {
      this.currentPage = num;
      // 重新请求数据
      this.loadinfo();
    },
    // 查询方法
    searchClicks() {
      this.paginationShow = false;
      this.currentChange(1);
      this.$nextTick(() => {
        this.paginationShow = true;
        this.loadinfo();
      });
    },
  },

  

  

  computed: {
    totalNum() {
      return this.total;
    }
  },
  watch: {
    // 监听总条数,每次查询将当前页置为 1
    totalNum(val) {
      if (val == (this.currentPage - 1) * this.pageSize && val != 0) {
        this.currentPage -= 1;
        this.search(this);
      }
    }
  }
};
</script>
<style scoped>
.locsty {
  font-size: 16px;
  font-family: "Microsoft YaHei";
}
.iconsty {
  color: #53b9fe;
  font-size: 22px;
}

.headstyle {
  border-bottom: 2px solid #e5e5e5;
  padding-bottom: 20px;
}

.middle {
  margin-top: 25px;
}

.selectcourse {
  float: left;
}
.download{
  color: white;
  text-decoration: none;
}
.editstyle{
  text-align: right;
}
.elbuttonedit{
  text-align: center;
}
</style>